package qlsl.androiddesign.view.subview.fragmentview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.demo.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.adapter.commonadapter.CommonGridAdapter;
import qlsl.androiddesign.adapter.subadapter.SquareImageAdapter;
import qlsl.androiddesign.fragment.subfragment.SquareFragment;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.indicatorview.AutoPlayManager;
import qlsl.androiddesign.view.indicatorview.IndicatorView;

/**
 * 广场页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class SquareView extends FunctionView<MainActivity> {

	private IndicatorView indicatorView;

	private AutoPlayManager autoBrocastManager;

	public SquareView(SquareFragment fragment, ViewGroup rootView, ViewGroup contentView) {
		super((MainActivity) fragment.getActivity());
		setContentView(rootView, contentView);
	}

	public void onTabResume() {
		((ScrollView) getContentView()).scrollTo(0, 0);
	}

	protected void initView(View view) {
		hideTitleBar();
		indicatorView = (IndicatorView) view.findViewById(R.id.net_indicate_view);
		indicatorView.setIndicateStyle(IndicatorView.INDICATE_ROUND_STYLE);
		indicatorView.setupLayoutByDefaultResource(5);
		indicatorView.show();
	}

	protected void initData() {
		startHomeImageManager();
		setHomeImages();
		setGridViewData();
		setListViewData();
	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.grid_item:
			doClickGridItem(view);
			break;
		case R.id.list_item:
			showToast("正在开发中");
			break;
		}
	}

	private void startHomeImageManager() {
		autoBrocastManager = new AutoPlayManager(indicatorView);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(Integer.MAX_VALUE);
		autoBrocastManager.setBroadcastTimeIntevel(6 * 1000, 6 * 1000);
		autoBrocastManager.loop();
	}

	private void setHomeImages() {
		List<String> urls = new ArrayList<String>();
		urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/db232799404acfaf8035114f26f5cdef.jpg");
		urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/ff71cbd940bc08e080f4c9aa674fa6e5.jpg");
		urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/c17da3d140cb7f23800df95df5dea925.jpg");
		urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/c11db0fd404fecd780e02c072eebaa43.jpg");
		urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/0cd50e764032212d80c25785135f1b15.jpg");
		indicatorView.setupLayout(urls);
		indicatorView.show();
	}

	private void setGridViewData() {
		String[] texts = new String[] { "诗友专访", "诗友趣闻", "诗友比赛" };
		int[] icons = new int[] { R.drawable.app_icon_default, R.drawable.app_icon_default,
				R.drawable.app_icon_default };
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int index = 0; index < texts.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("text", texts[index]);
			map.put("icon", icons[index]);
			list.add(map);
		}
		CommonGridAdapter adapter = new CommonGridAdapter(activity, list);
		GridView gridView = findViewById(R.id.gridView);
		gridView.setAdapter(adapter);
	}

	private void setListViewData() {
		List<String> urls = new ArrayList<String>();
		for (int index = 0; index < 3; index++) {
			urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/1e9673de407f5ef980f0478ebee24c79.jpg");
			urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/d3a7ddc8408f3e858023fbc4e25949bb.jpg");
			urls.add("http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/23f69832408d2333808d5e09e49a190d.jpg");
		}
		SquareImageAdapter adapter = new SquareImageAdapter(activity, urls);
		ListView listView = findViewById(R.id.listView);
		listView.setAdapter(adapter);
	}

	private void doClickGridItem(View view) {
		GridView gridView = findViewById(R.id.gridView);
		int position = gridView.getPositionForView(view);
		if (position == 0) {
			showToast("诗友专访正在开发中");
		} else if (position == 1) {
			showToast("诗友趣闻正在开发中");
		} else if (position == 2) {
			showToast("诗友比赛正在开发中");
		}
	}
}
